﻿<cfsilent>
	<cfsetting requesttimeout="300" />
	<cfflush interval="1024" />
	<cfscript>
		
		temFile = GetTempDirectory() & createUUID() & ".xls";

		studentUpdate = 0;
		studentInsert = 0;
		
		targetArgs = structNew();
		structInsert(targetArgs, "TabID", "labImport", true);
		
	</cfscript>
</cfsilent>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>导入分班数据</title>
<link href="<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/css/impoter.css" rel="stylesheet" type="text/css">
</head>

<body>

	<cfif isDefined("FORM.upload")>
		
		<!--- 将文件保存至临时目录 --->
		<cftry>
			<cffile action="upload" fileField="upload" destination="#temFile#" nameconflict="overwrite">
			<cfcatch type="application">
				<p class="error">上传的文件类型不正确, 请使用电子表格作为数据导入文件.</p>
			</cfcatch>
		</cftry>
		
		<!--- 将文件转换为电子表格对象 --->
		<cfif fileExists(temFile)>
			<cftry>
				<cfspreadsheet action="read" src="#temFile#" excludeHeaderRow="true" headerrow="1" query="rs" sheetname="学生分班信息">
				<cfcatch type="application">
					<p class="error">电子表格文件格式不正确, 请使用下载的模板文件录入学籍信息</p>
				</cfcatch>
			</cftry>
			
			<!--- 文件已成功转换为查询对象 --->
			<cfif isDefined("rs") and isQuery(rs)>
				
				<p class="success">尝试读取电子表格数据 ... 完成</p>
				
				<!--- 检查必要数据字段 --->
				<cfset columnChecked = true />
				<cfif not listFindNoCase(rs.columnList, "学号")><cfset columnChecked = false /></cfif>
				<cfif not listFindNoCase(rs.columnList, "姓名")><cfset columnChecked = false /></cfif>
				<cfif not listFindNoCase(rs.columnList, "班级")><cfset columnChecked = false /></cfif>
				
				<cfif not columnChecked>
					<p class="error">电子表格数据列不完整, 请检查模板文件是否包含以下必须字段信息: 学号,姓名,班级</p>
				</cfif>
				
				<cfif columnChecked>

					<p class="success">正在检查数据子项 ... 完成</p> 
					
					<cftransaction>
						
						<cfloop query="rs">
							
							<!--- 检查学号和姓名是否匹配 --->
							
                            <cfset sql  = "
											SELECT * FROM t_student a
											WHERE
												a.stu_id = :studentId 
												AND 
                                    			a.stu_name = :studentName "/>
                            <cfset queryObj = new Query( datasource=application.dnsMaster )/>
                            <cfset queryObj.addParam( name="studentId", value=rs["学号"][rs.currentRow], cfsqltype="cf_sql_varchar" )/>
                            <cfset queryObj.addParam( name="studentName", value=rs["姓名"][rs.currentRow], cfsqltype="cf_sql_varchar" )/>
                            <cfset rs_student = queryObj.execute( sql=sql ).getResult()/>
							<cfif rs_student.recordCount eq 1>

									<!--- 检查行政班名称是否正确 --->
                                    <cfset sql  = "SELECT a.cls_id 
													FROM t_class a
													WHERE a.cls_name = :clsName "/>
									<cfset queryObj = new Query( datasource=application.dnsMaster )/>
                                    <cfset queryObj.addParam( name="clsName", value=rs["班级"][rs.currentRow], cfsqltype="cf_sql_char" )/>
                                    <cfset rs_class = queryObj.execute( sql=sql ).getResult()/>
									
									<cfif rs_class.recordCount eq 1>
											<cfset sql = "UPDATE t_student a 
															SET a.cls_id = :clsId  
															WHERE 
																a.stu_id = :studentId 
																AND 
																a.stu_name = :studentName "/>
                                            <cfset queryObj = new Query( datasource=application.dnsMaster )/>
											<cfset queryObj.addParam( name="studentId", value=rs["学号"][rs.currentRow], cfsqltype="cf_sql_varchar" )/>
                                            <cfset queryObj.addParam( name="studentName", value=rs["姓名"][rs.currentRow], cfsqltype="cf_sql_varchar" )/>
                                            <cfset queryObj.addParam( name="clsId", value=rs_class.cls_id, cfsqltype="cf_sql_char" )/>
                                            <cfset queryObj.execute( sql=sql )/>
											
											<cfset studentUpdate++ />
											
										<cfelse>
											<p class="error">学生:<cfoutput>#rs["姓名"][rs.currentRow]#</cfoutput>(<cfoutput>#rs["学号"][rs.currentRow]#</cfoutput>) 班级: <cfoutput>#rs["班级"][rs.currentRow]#</cfoutput> 填写不正确, 无法找到对应行政班</p>
									</cfif>

								<cfelse>
									<p class="error">学生:<cfoutput>#rs["姓名"][rs.currentRow]#</cfoutput>(<cfoutput>#rs["学号"][rs.currentRow]#</cfoutput>) 填写不正确</p>
							</cfif>
							
						</cfloop>

					</cftransaction>
				
				</cfif>
			</cfif>
		</cfif> 
		
		<p>更新现有学籍数据 [<cfoutput>#studentUpdate#</cfoutput>] 条.</p>
		
		<a target="_parent" href="<cfoutput>#buildURL('classFlush')#</cfoutput>">刷新班级人数</a>
		
		<!---<a target="_parent" href="<cfoutput>#buildURL('programCourse', targetArgs)#</cfoutput>">重新导入课程</a>--->
		
		<!--- 删除临时文件 --->
		<cfif fileExists(temFile)>
			<cffile action="delete" file="#temFile#" />		
		</cfif>
	
	</cfif>

</body>
</html>